<?php

class SistemasController extends Zend_Controller_Action
{

    public function init()
    {
        
    }

    public function indexAction()
    {	
    	$login = new Zend_Session_Namespace('login');
		$rootmenu = $login->rootmenu;
		
    	$path = new Zend_Session_Namespace('path');
    	$url = $path->data;
    	
    	$pintar = new Libreria_Pintar();

    	$cn = new Model_Database_Datos();
    	$parametros[] = array('@cidtabl',$rootmenu);
    	$submenus = $cn->ejecutasql('bdaudit.dbo.pxw_listarobjetos', $parametros, true);
    	$csubmenus = count($submenus);

    	for($i=0; $i < $csubmenus; $i++){
    		if ($submenus[$i][1] == $rootmenu){
    			$cbosubmenus[$i] = array($submenus[$i][0],$submenus[$i][3]);	
    		}	
		}
		
		$val[] = array('#cbomenus',$pintar->ContenidoCombo($cbosubmenus,'9999999999'),'html');
		
		$evt[] = array('#btnbuscarpermisos', 'click','llenararbol();');
		$evt[] = array('#btnguardarcambios', 'click', 'guardarcambiosperm();');
//		$evt[] = array('#cbotipobus', 'change', 'combotipouser();');
		$evt[] = array('#btnbuscausuario', 'click', 'buscarusuario();');
		$evt[] = array('#txtuser', 'keypress', 'if(event.keyCode == 13){buscarusuario();}');
		
		$pintar->IniciaScript();
		$pintar->PintarValor($val);
		$pintar->PintarEvento($evt);
		$pintar->FinScript();

    }
    
    public function buscarusuarioAction()
    {
    $this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();

    		$ciduser = $this->_request->getPost('ciduser');
    		$vlogins = $this->_request->getPost('vlogins');
    		$ctipusr = $this->_request->getPost('ctipusr');
    		
    		$pintar = new Libreria_Pintar();
    		
    		$cn = new Model_Database_Datos();
    		$parametros[] = array('@ciduser', $ciduser);
    		$parametros[] = array('@vlogins', $vlogins);
    		$parametros[] = array('@ctipusr', $ctipusr);
    		$usuario = $cn->ejecutasql('bdaudit.dbo.pxw_buscausuario', $parametros, true);
    	
    		$cusuario = count($usuario);

    		if ($cusuario > 0){
    			$val[] = array('#spciduser', $usuario[0][0], 'html');
    			$val[] = array('#spvlogins', $usuario[0][1], 'html');
    			$pintar->IniciaScript();
    			$pintar->PintarValor($val);
    			$pintar->FinScript();
    		}else{
    			echo 'No se encontro usuario/grupo';
    		}
    	}
    }

    public function listaobjtreeAction()
    {
    	$this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();

    		$path = new Zend_Session_Namespace('path');
    		$url = $path->data;
    	
    		$iconallow = $url.'tree/images/drop-yes.gif';
    		//$icondeny = $url.'tree/images/drop-no.gif';
    		$icondeny = $url.'tree/images/denied.png';
    		
    		$cidtabl = $this->_request->getPost('cidtabl');
    		$menu = $this->_request->getPost('menu');
			$ciduser = $this->_request->getPost('ciduser');
			
    		$cn = new Model_Database_Datos();
    		$nombrestore = 'bdaudit.dbo.pxw_listarobjetos';
    		
    		$parametrosm[] = array('@cidtabl', $cidtabl);
    		$parametrosm[] = array('@ciduser', $ciduser);
    		$permisos = $cn->ejecutasql($nombrestore,$parametrosm, true);
    		$cpermisos = count($permisos);
    		
    		$b = false;
    		$arrayactions = null;
    		$arraymenus = null;
    		$arrayactionsl = null;
    		
    		$l = 0;
    		for ($i = 0; $i < $cpermisos; ++$i){
    			
    			if ($permisos[$i][1] == $cidtabl){
    				if ($permisos[$i][7] <> 0 ) $b = true;
    				
    				for ($j = 0; $j < $cpermisos; ++$j){
    					if ($permisos[$j][1] == $permisos[$i][0]){
    						if ($permisos[$j][7] <> 0 ) $b = true;
    						
    						for ( $k = 0; $k < $cpermisos; ++$k){
    							if($permisos[$k][1] == $permisos[$j][0]){
    								$arrayactionsl[] = array('text' => $permisos[$k][3], 'id' => $permisos[$k][0], 'depth' => '3', 'leaf' => true,'icon' => ($permisos[$k][7]== 0)? $icondeny:$iconallow, 'checked' => ($permisos[$k][7] == 0)?false:true,'estuvo' => ($permisos[$k][7] == 0)?false:true, 'children' => '' );
    							}
    						}
    						$carrayactionsl = count($arrayactionsl);
    						
    						$arrayactions[$l] = array('text' => $permisos[$j][3], 'id' => $permisos[$j][0], 'depth' => '2',  'leaf' => ($carrayactionsl>0)?false:true,'icon' => ($permisos[$j][7]== 0)? $icondeny:$iconallow, 'checked' => ($permisos[$j][7] == 0)?false:true,'estuvo' => ($permisos[$j][7] == 0)?false:true, 'children' => '' );
    						$arrayactions[$l]['children'] = $arrayactionsl;	
    						
    						$arrayactionsl = null;
    						++$l;
    					}
    				}
    				$carrayactions = count($arrayactions);
    				
    				$arraymenus[$i] = array('text' => $permisos[$i][3], 'id' => $permisos[$i][0], 'depth' => '1', 'leaf' => ($carrayactions>0)?false:true,'icon' => ($permisos[$i][7] == 0)? $icondeny:$iconallow, 'checked' => ($permisos[$i][7] == 0)?false:true, 'estuvo' => ($permisos[$i][7] == 0)?false:true,'children' => '' );
    				$arraymenus[$i]['children'] = $arrayactions;
    				$arrayactions = null;
    				$l = 0;
    			}
    		}
    		    	
			if ( $arraymenus != null){
				$arraymenus = array_values($arraymenus);
			}
	    	
    		
    		//cuando se llena los otros
    		$arrayraiz[0] = array ('text' => $menu, 'id' => $cidtabl, 'depth' => '0', 'leaf' => false, 'icon' => ($b) ? $iconallow: $icondeny, 'checked' => $b, 'estuvo' => ($b) ? true: false,'children' => '' );
    		$arrayraiz[0]['children'] = $arraymenus;
    		
    		$datos = json_encode($arrayraiz);
    		
    		$cont ='<script type="text/javascript"><!--
					Ext.BLANK_IMAGE_URL = "' . $url . 'tree/images/s.gif";
					Ext.EventManager.onDocumentReady(function() {
						tree = new Ext.tree.TreePanel(\'tree-div\',{
							animate:true,
							loader: new Ext.tree.CustomUITreeLoader({baseAttr:{uiProvider: Ext.tree.CheckboxNodeUI}}),
							enableDD:false,
							collapsible : true,
					        animCollapse: true,
							containerScroll: true,
							rootUIProvider: Ext.tree.CheckboxNodeUI,
							selModel:new Ext.tree.CheckNodeMultiSelectionModel(),
							rootVisible:false
						});
				
						// set the root node
						root = new Ext.tree.AsyncTreeNode({
									text: \'root\',
									draggable:false,
									id:\'source\',
									uiProvider: Ext.tree.CheckboxNodeUI,
									children: ' . $datos . '	
							    });
						
						tree.setRootNode(root);
					
						// render the tree
						tree.render();
						root.expand(false, false, function() {
							root.firstChild.expand(true);
						});
					});

				</script>';
    		echo $cont;
    	}
    }
    
	public function guardarpermisosAction(){
    	$this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();

    		$cadenapermisos = $this->_request->getPost('cadenapermisos');
    		$ciduser = $this->_request->getPost('ciduser');

    		$array1 = explode(',',$cadenapermisos);
    		$carray1 = count($array1);
    		
			$xml = '<d>';
    		for ($i = 0; $i < $carray1; ++$i){
    			$array2 = explode('^', $array1[$i]);
    			$xml .= '<r ';
    			$xml .= ' cidobje="'.$array2[0].'"';
    			//$xml .= ' ciduser="000000000779"';
    			$xml .= ' vanterior="'.$array2[1].'"';
    			$xml .= ' vactual="'.$array2[2].'"';
    			$xml .= ' ></r>';
    			$array2 = null;
    		}
    		$xml .= '</d>';
    		
    		$cn = new Model_Database_Datos();
    		$parametros[] = array('@cadenapermisos', $xml);
    		$parametros[] = array('@ciduser', $ciduser);
    		
    		$result = $cn->ejecutasql('bdaudit.dbo.pxw_asignarpermisos',$parametros, true);
    		echo $result[0][0];
    	}
    }
    
    public function objetoswebAction(){
    	$path = new Zend_Session_Namespace('path');
    	$url = $path->data;
    	
    	$login = new Zend_Session_Namespace('login');
		$rootmenu = $login->rootmenu;
		
    	$pintar = new Libreria_Pintar();
    	$cidtabl = $this->_request->getParam('cidtabl', '');
    	$nmenu = $this->_request->getParam('nmenu', '');
    	
    	$cn = new Model_Database_Datos();
    	$parametros[] = array('@cidtabl', $cidtabl);
    	
    	$objs = $cn->ejecutasql('bdaudit.dbo.pxw_listarobjetos', $parametros, true);
    	$cobjs = count($objs);
    	
    	$arrayx = null;
    	for($i = 0; $i < $cobjs; ++$i){
    		if($objs[$i][1] == $cidtabl){
    			$arrayx[] = $objs[$i];
    		}
    	}

 		$backbtn = ($cidtabl == $rootmenu)? '':'<a class="topoint" onclick="history.back();">Atras</a>' ;
 		
    	$val[] = array('#spinfo', $backbtn.' :: '.$cidtabl.' - '.$nmenu , 'html');
    	$val[] = array('#idparent', $cidtabl, 'val');
    	$val[] = array('#spadd', '<img class="topoint" src="'.$url.'images/add.ico" width="20px" height="20px"/>', 'html');
    	
    	$evt[] = array('#spadd', 'click', 'jsmantobj("1", "");');
    	
    	$dataobjetosw = json_encode($arrayx);
    	$pintar->IniciaScript();    	
    	$pintar->PintarValor($val);
    	$pintar->PintarEvento($evt);
    	$pintar->FinScript();
    	
   		$cont ='						
				<script type="text/javascript"> var members ='.$dataobjetosw.'</script>
				<script type="text/javascript">
					$(document).ready(function(){
						var optInit = getOptionPages(PagesObjetosweb);
						$("#Pagination").pagination(members.length, optInit);
					}); 
				</script>
		';
						
		echo $cont;    	
    }
    
    public function mantobjAction(){
    	$this->_helper->layout->disableLayout();
    	
    	$type = $this->_request->getParam('type', '');
    	$cidtabl = $this->_request->getParam('cidtabl', '');
    	$idsigma = $this->_request->getParam('idsigma', '');
    	
    	$pintar = new Libreria_Pintar();
    	
    	$hab[] = array('#txtidsigma', false);
    	$val[] = array('#txtcidtabl', $cidtabl, 'val');
    	$evt[] = array('#btncancel', 'click', 'closelight(true);');
    	$evt[] = array('#btnaceptar', 'click', 'xmantobjeto("'.$type.'");');
    	
    	if($type == '1'){
    		
    		$val[] = array('#txtidsigma', '____________', 'val');
    		
    		$hab[] = array('#txtcidtabl', false);
    		
    	}else{
    		$cn = new Model_Database_Datos();
    		$parametros[] = array('@idsigma', $idsigma);
    		$objeto = $cn->ejecutasql('bdaudit.dbo.pxw_objetoxid', $parametros, true);
    		
    		$val[] = array('#txtidsigma', $idsigma, 'val');
    		$val[] = array('#cboctipdat', $objeto[0][2],'val');
    		$val[] = array('#txtvdescri', $objeto[0][3], 'val');
    		$val[] = array('#txtvdoform', $objeto[0][4], 'val');
    		$val[] = array('#txtvobserv', $objeto[0][5], 'val');
    		$val[] = array('#txtnestado', $objeto[0][6], 'val');
    	}
    	
    	$pintar->IniciaScript();
    	$pintar->PintarValor($val);
    	$pintar->PintarEvento($evt);
    	$pintar->HabilitarComponente($hab);
    	$pintar->FinScript();
    }
    
    public function xmantobjAction(){
    	$this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();
    		
    		$pintar = new Libreria_Pintar();
    		
    		$type = $this->_request->getPost('type');
    		$idsigma = $this->_request->getPost('idsigma');
    		$cidtabl = $this->_request->getPost('cidtabl');
    		$ctipdat = $this->_request->getPost('ctipdat');
    		$vdescri = $this->_request->getPost('vdescri');
    		$vdoform = $this->_request->getPost('vdoform');
    		$vobserv = $this->_request->getPost('vobserv');
    		$nestado = $this->_request->getPost('nestado');
    		
    		$cn = new Model_Database_Datos();
    		
    		if($type == '1'){
    			
				$nombrestore = 'bdaudit.dbo.pxw_agregarobjeto';
    		}else{
    			$parametros[] = array('@idsigma', $idsigma);
    			$nombrestore = 'bdaudit.dbo.pxw_modificarobjeto';
    			$parametros[] = array('@nestado', $nestado);
    		}
    		$parametros[] = array('@cidtabl', $cidtabl);
    		$parametros[] = array('@viconos', $ctipdat);
    		$parametros[] = array('@vdescri', $vdescri);
    		$parametros[] = array('@vdoform', $vdoform);
    		$parametros[] = array('@vobserv', $vobserv);

    		$result = $cn->ejecutasql($nombrestore, $parametros, true);
    		
    		$val[] = array('#btncancel', 'Cerrar', 'html');
    		$hab[] = array('#btnaceptar', false);
    		
    		$pintar->IniciaScript();
    		$pintar->PintarValor($val);
    		$pintar->HabilitarComponente($hab);
    		$pintar->FinScript();
    		echo $result[0][0];
    	}
    }
    
    public function xdelobjAction(){
    	$this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();
    		
    		$idsigma = $this->_request->getPost('idsigma');
    		
    		$cn = new Model_Database_Datos();
    		$nombrestore = 'bdaudit.dbo.pxw_anularobjeto';
    		$parametros[] = array('@idsigma', $idsigma);
    		
    		$result = $cn->ejecutasql($nombrestore, $parametros, true);
    		
    		echo $result[0][0];
    	}
    }
    
 public function cambiohostcajerosAction(){
    	$pintar=new Libreria_Pintar();
    	    	
        $login = new Zend_Session_Namespace('login');
    	$codarea= $login->codarea;
        
    	$evt[]=array('#btnbuscarusuarioaignar','click','frmbusqusuarios();');
    	
    	$datos=new Model_Database_Datos();    
    		
    	$datos2=new Model_Database_Datos();
		
    	$datos3=new Model_Database_Datos();

    	$tabla3=$datos3->ejecutasql('dbo.[area]','',true);            
    	$val[]=array('#cboarea',$pintar->ContenidoCombo($tabla3,'99999999'),'html');  
    	
    	$evt[]=array('#cboarea','change','filtrarusuarioareas();');
    	
		$array[]=array('CAJA1','CAJA1');
    	$array[]=array('CAJA2','CAJA2');
    	$array[]=array('CAJA3','CAJA3');
    	$array[]=array('CAJA4','CAJA4');
    	$array[]=array('CAJA5','CAJA5');
    	$array[]=array('CAJA6','CAJA6');
    	$array[]=array('CAJA7','CAJA7');
    	$array[]=array('CAJA8','CAJA8');
    	$array[]=array('CAJAMAYTA','CAJAMAYTA');
    	$array[]=array('CAJAMAYTA2','CAJAMAYTA2');
    	$array[]=array('CAJAALISOS','CAJAALISOS');
    	
    	
    	if($codarea=='017'){
    		$fun[]=array('$("#trarea").hide();');
    	    $tabla=$datos->ejecutasql('usp_listar_cajeros','',true);            
    	    $val[]=array('#cbocajero',$pintar->ContenidoCombo($tabla,'99999999'),'html');  
    		
    	 
    	$parametros2[]=array('@tipo','3');
    	$tabla2=$datos2->ejecutasql('usp_listar_cajeros',$parametros2,true);
    	
   	
    	
    	$result = array_merge($array, $tabla2);
    	
    	$val[]=array('#cbohostasignado',$pintar->ContenidoCombo($result,'99999999'),'html');
    	
    	   	
    		}
    	else{   	

    		$fun[]=array('$("#trarea").show();');    		
    		
    		$parametros[]=array('@tipo','4');
    		$tabla=$datos2->ejecutasql('usp_listar_cajeros',$parametros,true);    		
    		
    	
    	
    	$result = array_merge($array, $tabla);
    	
    	$val[]=array('#cbohostasignado',$pintar->ContenidoCombo($result,'99999999'),'html');
    		
    		
    		
    	}
    	
    	$evt[]=array('#cbocajero','change','mostrarhostactualcajeros();');
    	
    	
    	
    	
    	$evt[]=array('#btngrabar','click','cambiarhostcajeros();');    	 	
    	
    	$pintar->IniciaScript();
    	$pintar->PintarValor($val);
    	$pintar->PintarEvento($evt);
    	$pintar->EjecutarFuncion($fun);
    	$pintar->FinScript();
    	
    	
    }
    
    public function buscarhostcajerosAction(){
    	 	$this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();
    		
    		$cusuari=$this->_request->getPost('cusuari');
    		
    		
    		$datos=new Model_Database_Datos();
    		$nombrestore='usp_listar_cajeros';
    		$parametros[]=array('@tipo','2');
    		$parametros[]=array('@cusuari',$cusuari);

    		$tabla=$datos->ejecutasql($nombrestore,$parametros,true);
    		
    		
    		$val[]=array('#txthostactual',$tabla[0][1],'val');
    		    		
    		$pintar=new Libreria_Pintar();
    		$pintar->PintarValor($val);
    	}   	    	
    }
    
    public function cambiarusuariocajeroAction(){
        $this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();
    		
    		$cusuari=$this->_request->getPost('cusuari');
    		$hostname=$this->_request->getPost('hostname');
    		$static=$this->_request->getPost('permanente');
    		$login = new Zend_Session_Namespace('login');
    			
    		$datos=new Model_Database_Datos();
    		$nombrestore='usp_cambiarhost';    		
    		$parametros[]=array('@cusuari',$cusuari);
    		$parametros[]=array('@dhostname',$hostname);
    		$parametros[]=array('@static',$static);
    		$parametros[]=array('@p_userweb',$login->codusuario);
    		
    		$tabla=$datos->ejecutasql($nombrestore,$parametros,true);
    		
    		
    	}
    	
    }
    
    public function  filtrarusuarioAction(){
    	    $this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();
    		
    		$careas=$this->_request->getPost('careas');    		
    		$datos=new Model_Database_Datos();
    		
    		$parametros[]=array('@tipo','5');
    		$parametros[]=array('@careas',$careas);
    		
    		$tabla=$datos->ejecutasql('usp_listar_cajeros',$parametros,true);
    		
    		//print_r($tabla);
    		

    		$pintar=new Libreria_Pintar();    		
    		$val[]=array('#cbocajero',$pintar->ContenidoCombo($tabla,'99999999'),'html');
    		//$val[]=array('#cbohostasignado',$pintar->ContenidoCombo($tabla,'99999999'),'html');

    		    	
        	$login = new Zend_Session_Namespace('login');
    		$codarea= $login->codarea;
    		
    		if ($codarea=='025'){
    		$array[]=array('CAJA1','CAJA1');
			$array[]=array('CAJA2','CAJA2');
			$array[]=array('CAJA3','CAJA3');
			$array[]=array('CAJA4','CAJA4');
			$array[]=array('CAJA5','CAJA5');
			$array[]=array('CAJA6','CAJA6');
			$array[]=array('CAJA7','CAJA7');
			$array[]=array('CAJA8','CAJA8');
			$array[]=array('CAJAMAYTA','CAJAMAYTA');
			$array[]=array('CAJAMAYTA2','CAJAMAYTA2');
			$array[]=array('CAJAALISOS','CAJAALISOS');
    		$array[]=array('PLATAFORMA19','PLATAFORMA19'); 		
    		
    	
      		
    			
    		$datos2=new Model_Database_Datos();
    		$parametros2[]=array('@tipo','4');
    		$tabla2=$datos2->ejecutasql('usp_listar_cajeros',$parametros2,true);  
    		
    		$result = array_merge($array, $tabla2);
    		  		
    		$val[]=array('#cbohostasignado',$pintar->ContenidoCombo($result,'99999999'),'html');
    		
    		}
    		
    		$pintar->PintarValor($val);
    		
    	}
    	
    }
    
}

